import { useEffect, useState, useRef } from 'react';
import { roleSelectBox, fetchRoleMenus, queryDeptTree } from '@/services/ant-design-pro/system';

export default function Page() {
  const [role, setRole] = useState([]);
  const [menu, setMenu] = useState([]);
  const [dept, setDept] = useState([]);
  const [loading, setLoading] = useState(true);
  const [refresh, setRefresh] = useState(0); // 用于触发重新获取数据的 state

  useEffect(() => {
    setLoading(true);
    roleSelectBox().then((res) => {
      const r = res.data.map((item: any) => {
        return {
          value: item.rolesCode,
          label: item.roleName,
        };
      });
      setRole(r);
      setLoading(false);
    });
    fetchRoleMenus({}).then((res) => {
      setMenu(res.data);
    });
    queryDeptTree({}).then((res) => {
      setDept(res.data);
      console.log(res.data, '---res.data');
    });
  }, [refresh]); // 依赖 refresh 来触发重新获取数据

  return {
    role,
    menu,
    dept,
    loading,
    refreshData: () => setRefresh(prev => prev + 1), // 
  };
};